#!/usr/bin/python
# -*- coding:utf-8 -*-

import sys
import tmuxbk.controller as controller
import tmuxbk.config as config
import tmuxbk.util as util
import logging
import tmuxbk.log as log

"""
the start script of retmux
"""

help_info = """ %(cyan)s
                  __
       ________  / /_____ ___  __  ___  __
      / ___/ _ \/ __/ __ `__ \/ / / / |/_/
     / /  /  __/ /_/ / / / / / /_/ />  <  
    /_/   \___/\__/_/ /_/ /_/\__,_/_/|_|

            - A tmux backup/reload tool %(clear)s

    [%(bold)sUSAGE%(clear)s] 
        retmux [OPTIONS] 

    [%(bold)sOPTIONS%(clear)s] 
        %(bold)s-h%(clear)s print help message

        %(bold)s-v%(clear)s version

        %(bold)s-l%(clear)s [name] : list backup info
               with [name]: show detailed backup info by name
            without [name]: show brief and detailed info interactively

        %(bold)s-d%(clear)s [name] : delete a backup
               with [name]: delete by given name
            without [name]: delete interactively

        %(bold)s-b%(clear)s [name] : backup current tmux sessions
               with [name]: name the backup with given name
            without [name]: name the backup with default name(timestamp)

        %(bold)s-r%(clear)s [name] : restore tmux sessions from backup
               with [name]: restore sessions by backup name
            without [name]: restore from the latest backup

        %(bold)s-ri%(clear)s       : restore sessions interactively
        config file: $HOME/.retmux/retmux.conf

    [%(bold)sAUTHOR%(clear)s] 
        Kai Yuan <kent.yuan(at)gmail.com> 
        Please report bugs at https://github.com/sk1418/retmux/issues
    """
def usage():
    """print usage information"""
    print help_info % log.STYLE

def version():
    """print version"""
    print 'retmux ' + config.VERSION

actions = {
    '-l'  : controller.show_and_action,
    '-b'  : controller.do_backup, 
    '-d'  : controller.do_delete,
    '-r'  : controller.do_restore,
    '-ri'  : controller.interactive_restore,
    '-h'  : usage,
    '-v'  : version
}


if __name__ == '__main__':

    #load config
    if not config.load_config():
        print "loading config failed"
    
    log.setup_log(config.LOG_LVL_CONSOLE, config.LOG_LVL_FILE)

    #process user input options
    if sys.argv.__len__() <2 or not sys.argv[1]:
        usage()
        sys.exit(1)
    else: 
        func = actions.get(sys.argv[1])

        #backup or restore
        if sys.argv[1] in ('-b','-r','-l', '-d') \
                and sys.argv.__len__() == 3 :
            func(sys.argv[2]) 
        else:
            func() if func else usage()
            sys.exit(0)

